﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DC2010;
using DC2010.Objects.Common;

namespace DC2010_EditorInfo.Controls
{
    public partial class KeyValueUC : UserControl
    {
        public List<KeyValue> Data { get; set; }

        public KeyValueUC()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Data.Clear();
            Root.log.InfoFormat("KeyValue collection cleaned");

            string txt = textBox1.Text.Replace("\r", "");
            string[] lines = txt.Split('\n');

            foreach (string line in lines)
            {
                string[] keyvaluepair = line.Split(';');

                if (keyvaluepair.GetLength(0) == 2)
                {
                    KeyValue kv = new KeyValue();
                    kv.Key = keyvaluepair[0];
                    kv.Value = keyvaluepair[1];

                    Data.Add(kv);
                    Root.log.InfoFormat("Succesfully added KeyValue with Key:{0} and Value:{1}", kv.Key, kv.Value);

                }
            }
        }

        public void FillList()
        {

            string completetext = "";

            if (Data != null)
            {
                foreach (KeyValue keyValue in Data)
                {
                    completetext += string.Format("{0};{1}\r\n", keyValue.Key, keyValue.Value);
                }
            }
            else
            {
                Root.log.Warn("Adding to list failed. No effects.");
            }

            textBox1.Text = completetext;
        }

        private void KeyValueUC_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
                FillList();
        }
    }
}
